package com.act.web;

import java.util.List;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.act.domain.Employee;
import com.act.domain.Reward;
import com.act.dto.PagerDTO;
import com.act.dto.Response;
import com.act.service.api.IEmployeeService;
import com.act.service.api.IRewardService;
import com.act.util.LoggerUtil;
import com.act.vo.PagerVo;

/**
 * @Desccription:类说明
 * @Author: 刘承军
 * @E-mail:923050345@qq.com
 * @Date:2017年11月6日 下午2:20:08
 */
@RestController
@RequestMapping("/reward")
public class RewardController {

    @Resource
    private IRewardService rewardService;

    @Resource
    private IEmployeeService employeeService;

    private final Logger LOGGER = Logger.getLogger(RewardController.class);

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Response add(Reward reward) {
        try {
            List<Employee> employees = employeeService.getEmployeesByEmployeeIdOrName(1,null, reward.getEmployee().getName());
            Employee emp = new Employee();
            emp.setId(employees.get(0).getId());
            emp.setName(employees.get(0).getName());
            reward.setEmployee(emp);
            rewardService.add(reward);
            return Response.success();
        } catch (Exception e) {
            return Response.fail();
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Response update(Reward reward) {
        try {
            rewardService.update(reward);
            return Response.success();
        } catch (Exception e) {

            return Response.fail();
        }
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Response delete(@PathVariable int id) {
        try {
            rewardService.delete(id);
            return Response.success();
        } catch (Exception e) {

            return Response.fail();
        }
    }

    @RequestMapping(value="/get",method = RequestMethod.POST)
    @ResponseBody
    public Response getAll(@RequestBody PagerDTO pagerDTO){
        try {
            PagerVo<Map> amlist=rewardService.getAll(pagerDTO);

            return Response.success(amlist);
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, e);
        }
        return Response.fail();
    }

    @RequestMapping(value="/get/{id}",method = RequestMethod.GET)
    @ResponseBody
    public Response getById(@PathVariable int id){
        try {
            Reward reward = rewardService.getById(id);

            return Response.success(reward);
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, e);
        }
        return Response.fail();
    }

    @RequestMapping(value="/getByJobnum/{jobnum}",method = RequestMethod.POST)
    @ResponseBody
    public Response getByJobnum(@PathVariable String jobnum,@RequestBody PagerDTO pagerDTO){
        try {
            PagerVo<Map> amlist=rewardService.getRewardByJobnum(pagerDTO, jobnum);

            return Response.success(amlist);
        } catch (Exception e) {
            LoggerUtil.error(LOGGER, e);
        }
        return Response.fail();
    }
}
